package com.hlf.modules.merchant.table.service.impl;

import com.hlf.common.constants.Constants;
import com.hlf.common.exception.BusinessException;
import com.hlf.common.utils.PageUtils;
import com.hlf.common.utils.PropertieUtil;
import com.hlf.common.utils.Query;
import com.hlf.common.utils.SendMailUtils;
import com.hlf.modules.h5.interceptor.AuthorizationInterceptor;
import com.hlf.modules.merchant.table.entity.Table;
import com.hlf.modules.merchant.table.mapper.TableMapper;
import com.hlf.modules.merchant.table.service.TableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/15.
 */
@Service("TableService")
public class TableServiceImpl implements TableService {

    @Autowired
    private TableMapper tableMapper;
    @Resource
    private JavaMailSenderImpl sender;
    @Autowired
    PropertieUtil propertieUtil;

    @Override
    public PageUtils queryListForPage(Map<String, Object> map) {
        Query query = new Query(map);
        int total = tableMapper.queryTotal(query);
        List<Table> tableList = null;
        if (total > 0) {
            tableList = tableMapper.queryList(query);
        }
        return new PageUtils(tableList, total, query.getLimit(), query.getPage());
    }

    /**
     * 添加餐桌
     *
     * @param table
     * @return
     */
    @Override
    public Integer insertSelective(Table table) {
        return tableMapper.insertSelective(table);
    }

    /**
     * 根据id查Table
     *
     * @param id
     * @return
     */
    @Override
    public Table queryObjectForId(Long id) {
        return tableMapper.queryObject(id);
    }

    /**
     * 根据主键修改
     *
     * @param table
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(Table table) {
        return tableMapper.updateByPrimaryKeySelective(table);
    }

    /**
     * 根据餐桌类型id查总数
     *
     * @param tableTypeId
     * @return
     */
    @Override
    public int queryCountForTableTypeId(long tableTypeId) {
        return tableMapper.queryTotalForTableTypeId(tableTypeId);
    }

    /**
     * 更新餐桌状态
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public synchronized Table updateTableStatus(Long id) {

        Table table = tableMapper.queryObject(id);
        if (table.getTableStatus() != Constants.TABLE_STATUS_FREE) {
            throw new BusinessException("抱歉,该餐桌已被使用!");
        }
        table.setTableStatus(Constants.TABLE_STATUS_ORDER);
        int i = tableMapper.updateByPrimaryKeySelective(table);
        if (i == 0) {
            throw new BusinessException("餐桌选择失败!");
        }
        return table;
    }

    /**
     * 发送邮件
     *
     * @param email
     */
    @Override
    public void sendEmail(String email,String storeName) {
        String[] ss = {email};
        String mailBusinessName = propertieUtil.getPropertie("MAIL_BUSINESS_NAME");
        String mailType = propertieUtil.getPropertie("MAIL_TYPE");
        String text = propertieUtil.getPropertie("EMAIL_TEXT");
        String now = LocalDate.now().toString();
        try {
            SendMailUtils.sendTextWithHtml(sender,ss,mailBusinessName + "_" + storeName + "_" + mailType + "_" + now,
                    "<html><head></head><body>"+ text +
                            "<img src='https://img12.360buyimg.com/babel/jfs/t10027/113/1976061558/158891/4a16c48/59eae7feN4f6e6bd9.jpg'/></body>");
        } catch (Exception e) {
            throw new BusinessException("邮件发送失败!");
        }
    }

    /**
     * 根据餐桌编号查找餐桌(模糊查询)
     *
     * @param tableNum
     * @return
     */
    @Override
    public List<Table> serachByTableNum(String tableNum, Long storeId) {
        return tableMapper.serachByTableNum(tableNum, storeId);
    }

    /**
     * 删除餐桌
     *
     * @param request
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized void deleteTable(HttpServletRequest request, List<String> tableIds) {

        Long userId = (Long) request.getAttribute(AuthorizationInterceptor.LOGIN_USER_KEY);
        for (String tableId : tableIds) {
            Table table = queryObjectForId(Long.parseLong(tableId));
            if ( !Constants.TABLE_STATUS_FREE.equals(table.getTableStatus())) {
                throw new BusinessException("餐桌正在使用!");
            }
            table.setLastUpdateBy(userId);
            table.setDelFlag(Constants.DELETE_FLAG_UNABLE);
            int i = updateByPrimaryKeySelective(table);
            if (i == 0) {
                throw new BusinessException("删除失败!");
            }
        }
    }

}
